package com.mdp.sms.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.core.utils.DateUtils;
import com.mdp.sms.tencent.client.SmsTemplateApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.mdp.core.service.BaseService;
import static com.mdp.core.utils.BaseUtils.*;
import com.mdp.core.entity.Tips;
import com.mdp.core.err.BizException;

import com.mdp.sms.entity.SmsTemplate;
import com.mdp.sms.mapper.SmsTemplateMapper;
import org.springframework.util.ObjectUtils;

/**
 * @author maimeng-mdp code-gen
 * @since 2024-3-6
 */
@Service
public class SmsTemplateService extends BaseService<SmsTemplateMapper,SmsTemplate> {
	static Logger logger =LoggerFactory.getLogger(SmsTemplateService.class);


	@Autowired
	private SmsTemplateApi smsTemplateApi;
	/**
	 * 添加模板
	 * @author lyk
	 * @cdate 2019/9/16 18:35
	 */
	public SmsTemplate addTemplate(SmsTemplate smsTemplate){
		Map<String,Object> addParams=new HashMap();
		addParams.put("type", Integer.valueOf(smsTemplate.getType()));
		addParams.put("title",smsTemplate.getTitle());
		addParams.put("text", smsTemplate.getText());
		addParams.put("international", Integer.valueOf(smsTemplate.getInternational()));
		addParams.put("remark", smsTemplate.getRemark());
		Map<String,Object> result=smsTemplateApi.addSmsTemplate(addParams);
		if(!ObjectUtils.isEmpty(result.get("result"))&&(Integer)result.get("result")==0) {
			JSONObject j=(JSONObject)result.get("data");
			if(!ObjectUtils.isEmpty(j.get("id"))) {
				smsTemplate.setId(j.get("id").toString());
			}
			Integer status=(Integer)j.get("status");
			smsTemplate.setStatus(status.toString());
			smsTemplate.setCreateDate(DateUtils.getNowDate());
			this.insert(smsTemplate);
		}
		return smsTemplate;
	}

	/**
	 * 删除模板
	 * @author lyk
	 * @cdate 2019/9/16 18:35
	 */
	public void delTemplate(SmsTemplate smsTemplate) {
		Map<String,Object> delParams=new HashMap();
		List<Integer> tplIdList=new ArrayList();
		tplIdList.add(Integer.valueOf(smsTemplate.getId()));
		delParams.put("tpl_id", tplIdList);
		Map<String,Object> result=smsTemplateApi.delSmsTemplate(delParams);
		this.deleteByPk(smsTemplate);
	}

	/**
	 * 编辑短信
	 * @author lyk
	 * @cdate 2019/9/17 11:47
	 */
	public void editTemplate(SmsTemplate smsTemplate) {
		Map<String,Object> params=new HashMap();
		params.put("remark", smsTemplate.getRemark());
		params.put("text", smsTemplate.getText());
		params.put("title", smsTemplate.getTitle());
		params.put("tpl_id", Integer.valueOf(smsTemplate.getId()));
		params.put("type", Integer.valueOf(smsTemplate.getType()));
		Map<String,Object> result=smsTemplateApi.modifySmsTemplate(params);
		if(!ObjectUtils.isEmpty(result.get("result"))&&(Integer)result.get("result")==0) {
			JSONObject j=(JSONObject)result.get("data");
			Integer status=(Integer)j.get("status");
			smsTemplate.setStatus(status.toString());
			this.updateSomeFieldByPk(smsTemplate);
		}
	}


	/**
	 * 定时查询腾讯云的短信模板的状态
	 * @throws Exception
	 */
	@Scheduled(cron = "0 0/3 * * * ?")
	public void selectTemplate() throws Exception {
		String strDateFormat = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
		SmsTemplate customerQuerySmsTemplateObject=new SmsTemplate();
		customerQuerySmsTemplateObject.setStatus("1");
		List<SmsTemplate> smsTemplateList=this.selectListByWhere(customerQuerySmsTemplateObject);
		/*customerQuerySmsTemplateObject.setStatus("2");
		smsTemplateList.addAll(this.selectListByWhere(customerQuerySmsTemplateObject));*/
		for(SmsTemplate s:smsTemplateList) {
			Map<String,Object> params=new HashMap();
			List<Integer> tplIdList=new ArrayList();
			tplIdList.add(Integer.valueOf(s.getId()));
			params.put("tpl_id", tplIdList);
			Map<String,Object> result=smsTemplateApi.getSmsTemplate(params);
			if(!ObjectUtils.isEmpty(result.get("result"))&&(Integer)result.get("result")==0) {
				List<JSONObject> dataJSONObjectList=(List<JSONObject>)result.get("data");
				for(JSONObject j:dataJSONObjectList) {
					Integer status=(Integer)j.get("status");
					String reply=(String)j.get("reply");
					String replyTimeStr=(String)j.get("reply_time");
					s.setReply(reply);
					s.setStatus(status.toString());
					s.setReplyDate(sdf.parse(replyTimeStr));
					this.updateSomeFieldByPk(s);
				}
			}
		}
	}

	/**
	 * 自定义查询，支持多表关联
	 * @param page 分页条件
	 * @param ew 一定要，并且必须加@Param("ew")注解
	 * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
	 * @return
	 */
	public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
		return baseMapper.selectListMapByWhere(page,ew,ext);
	}
}

